package tushu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.cj.protocol.Resultset;

import tushu.Bean.BookBean;
import tushu.Bean.BookRentJiLuBean;
import util.JDBCUtils;

public class tushuDao {
 Connection con=null;
 PreparedStatement ps=null;

 public String insert(BookBean book) {
	 String sql="insert into b_book(id,bookname,number,address,rentcishu)"+
			 	 "values(?,?,?,?,?)";
	 try {
		con=JDBCUtils.getConnection();
		ps=con.prepareStatement(sql);
		ps.setInt(1, book.getId());
		ps.setString(2, book.getBookName());
		ps.setInt(3, book.getNumber());
		ps.setString(4, book.getAddress());
		ps.setInt(5, book.getRentCiShu());
		return ps.executeUpdate()+"";
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return sql;
 }
 
 public void UpdateCiShuNumber() {
String sql="update b_rentjilu set ";	 
	 try {
		con=JDBCUtils.getConnection();
		ps=con.prepareStatement(sql);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	 
 }
 
 public BookBean selectByName(BookBean book) {
	 String sql="select *from b_book where bookName=?";
	 ResultSet rs=null;
	 BookBean b=new BookBean();
	 try {
		con=JDBCUtils.getConnection();
		ps=con.prepareStatement(sql);
		ps.setString(1, book.getBookName());
		rs=ps.executeQuery();
		while(rs.next()) {
		b.setId(rs.getInt("id"));
		b.setBookName(rs.getString("bookName"));
		b.setNumber(rs.getInt("number"));
		b.setAddress(rs.getString("address"));
		b.setRentCiShu(rs.getInt("rentcishu"));
		}
		return b;
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return b;
	 
 }
	public int update(BookBean book) {
		String sql="update b_book set rentcishu=?,number=? where bookName=?";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, book.getRentCiShu());
			ps.setInt(2, book.getNumber());
			ps.setString(3, book.getBookName());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public int updateKuCun(BookBean book) {
		String sql="update b_book set number=? where bookName=?";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, book.getNumber());
			ps.setString(2, book.getBookName());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public List<BookBean> selectAll(){
		String sql="select * from b_book";
		ResultSet rs=null;
		List<BookBean> list=new ArrayList<>();
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				BookBean book=new BookBean();
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("bookName"));
				book.setNumber(rs.getInt("number"));
				book.setAddress(rs.getString("address"));
				book.setRentCiShu(rs.getInt("rentcishu"));
				list.add(book);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
 
}
